import pygame.image as pi
from random import randint
from lib.ZSprite import ZSprite
from res.glo import src
from res.conf import enemyspeedmin, enemyspeedmax, window_size, enemyscore, enemyhealth, enemyhitdamage
from res.fun import _random_pos
from addons.healthbar import HealthBar


class Enemy(ZSprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.score = enemyscore
        self.health = self.maxhealth = enemyhealth
        self.hitdamage = enemyhitdamage
        self.isRunning = True
        self.image = pi.load(src['image']['enemy'])
        self.rect = self.image.get_rect()
        self.speed = randint(enemyspeedmin, enemyspeedmax)
        self.rect.topleft = _random_pos(window_size[0], -600)
        # addons
        self.healthbar = HealthBar(self.health)

    def draw(self):
        if self.isRunning:
            self.screen.blit(self.image, self.rect.topleft)
            if not self.health == self.maxhealth :
                self.healthbar.draw(self.health)

    def update(self):
        if self.isRunning:
            self.rect.y += self.speed
            self.healthbar._pos = self.rect.x + self.healthbar.marginleft, self.rect.y - self.healthbar.margintop

    def hit(self, bullet):
        self.health -= bullet.hit
        if self.health <= 0:
            self.die()

    def die(self):
        self.healthbar.isRunning = False
        self.isRunning = False
